package dataDAOImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import dataConnector.Connector;
import dataDAOInterfaces.DALException;
import dataDAOInterfaces.IRecipeDAO;
import dataDTO.RecipeDTO;

public class MySQLRecipeDAO implements IRecipeDAO {

	@Override
	public RecipeDTO getRecipe(int recipeId) throws DALException {
			ResultSet rs = Connector.doQuery("SELECT*FROM recept WHERE recept_id = " +recipeId);
			try {
				if(!rs.first()) throw new DALException("The recipte " + recipeId + " dont exist");
				return new RecipeDTO(rs.getInt(1), rs.getString(2));
			}
		catch (SQLException e) {throw new DALException(e);}
		
	}

	@Override
	public List<RecipeDTO> getRecipeList() throws DALException {
		List<RecipeDTO> list = new ArrayList<RecipeDTO>();
		ResultSet rs = Connector.doQuery("SELECT*FROM recept");
		try{
			while (rs.next())
			{
				list.add(new RecipeDTO(rs.getInt(1),rs.getString(2)));
			}
		}
		catch (SQLException e) { throw new DALException(e);}
		return list;
	}

	@Override
	public void createRecipe(RecipeDTO recept) throws DALException {
		Connector.doUpdate("INSERT INTO recept(recept_id, recept_navn) VALUES" +
				 "(" + recept.getRecipeId() + ",'" + recept.getRecipeName() + "')");
		
	}

	@Override
	public void updateRecipe(RecipeDTO recept) throws DALException {
				Connector.doUpdate("UPDATE recept SET recept_id = '" + recept.getRecipeId() +
						"', recept_navn = '" + recept.getRecipeName() + "' WHERE recept_id = " + recept.getRecipeId());
	}

}
